class TrainrouterController < ApplicationController
  before_filter :sys_before_log

  def update
    begin
      trainRouters = TrainRouter.find_all_by_train_id(params[:id])              #根据id找到对象
      for trainRouter in trainRouters                                           #删除掉所有关于此列车的router
        trainRouter.destroy
      end
      if params[:firstRouterId]
        trainRouter = TrainRouter.new
        trainRouter.router_id = params[:firstRouterId]
        trainRouter.train_id = params[:id]
        trainRouter.sequence = 0
        trainRouter.save!
      end
      if params[:secondRouterId]
        trainRouter = TrainRouter.new
        trainRouter.router_id = params[:secondRouterId]
        trainRouter.train_id = params[:id]
        trainRouter.sequence = 1
        trainRouter.save!
      end
      if params[:thirdRouterId]
        trainRouter = TrainRouter.new
        trainRouter.router_id = params[:thirdRouterId]
        trainRouter.train_id = params[:id]
        trainRouter.sequence = 2
        trainRouter.save!
      end
      begin
        train = Train.find params[:id]
        #        train.router_date = Time.now.to_date                                    #得到当前日期
        train.router_date = params[:router_date]
        train.router_number = params[:router_number]
        train.save!
        info = 'success'
      rescue Exception=>e
        info = e.to_s
      end

    rescue Exception=>e
      info = e.to_s
    end
    result = "{success:true,info:\"#{info}\"}"
    render :text=>result, :layout=>false
  end

end
